package livesun.justiceeternal.db.love;
import java.util.List;
import livesun.justiceeternal.db.IModule;
import livesun.justiceeternal.db.task.DbScheduler;

public class LoveModule extends IModule<ILoveListener> {

    private static LoveModule sInstance = null;

    private LoveModule() {
    }

    public static LoveModule getInstance() {
        if (sInstance == null) {
            synchronized (LoveModule.class) {
                if (sInstance == null) {
                    sInstance = new LoveModule();
                }
            }
        }
        return sInstance;
    }

    @Override
    protected void onLoaderStart() {
        setLoaderFinished();
    }

    public void delete(LoveBean loveBean){
        LoveDeleteTask<LoveBean> loveDeleteTask = new LoveDeleteTask<LoveBean>(LoveBean.class,loveBean){
            @Override
            public void deleteError(String error) {
                super.deleteError(error);
                notifyDeleteChanged(error);
            }

            @Override
            public void deleteSuccess(LoveBean loveBean) {
                super.deleteSuccess(loveBean);
                notifyDeleteChanged(loveBean);
            }
        };

        DbScheduler.submit(loveDeleteTask);
    }

    private void notifyDeleteChanged(String error){
        for (ILoveListener iLoveListener : getListeners()) {
            iLoveListener.onDeleteError(error);
        }
    }

    private void notifyDeleteChanged(LoveBean loveBean){
        for (ILoveListener iLoveListener : getListeners()) {
            iLoveListener.onDeleteSucess(loveBean);
        }
    }

    /**
     * 查询doc
     */
    public void queryDocList(){
        LoveQueryTask<LoveBean> absQueryTask = new LoveQueryTask<LoveBean>(LoveBean.class){
            @Override
            public void onQueryFinished(List<LoveBean> loveBeans) {
                super.onQueryFinished(loveBeans);
                notifyNoteListChanged(loveBeans);
            }
        };
        DbScheduler.submit(absQueryTask);
    }

    public void saveLove(LoveBean loveBean){
        LoveSaveTask<LoveBean> loveSaveTask = new LoveSaveTask<LoveBean>(LoveBean.class,loveBean){

            @Override
            public void onSaveSuccess(LoveBean loveBean) {
                super.onSaveSuccess(loveBean);
                notifyListChanged(loveBean);
            }

            @Override
            public void onSaveFailed(String erro) {
                super.onSaveFailed(erro);
                notifyListChanged(erro);
            }
        };
        DbScheduler.submit(loveSaveTask);
    }

    private void notifyListChanged(LoveBean loveBean){
        for (ILoveListener listener : getListeners()) {
            listener.onInsertSuccess(loveBean);
        }
    }

    private void notifyListChanged(String erro){
        for (ILoveListener listener : getListeners()) {
            listener.onInsertError(erro);
        }
    }

    private void notifyNoteListChanged(List<LoveBean> loveBeans) {
        for (ILoveListener listener : getListeners()) {
            listener.onQueryFinish(loveBeans);
        }
    }
}
